package JDK8Time;

import java.time.*;
import java.time.format.DateTimeFormatter;

public class Test {
    public static void main(String[] args) {
        LocalDate ld =  LocalDate.now();
        System.out.println(ld);

        //获取单一时间
        int year = ld.getYear();
        int month = ld.getMonthValue();
        int day = ld.getDayOfMonth();
        int dayFoYear = ld.getDayOfYear();
        int dayFoWeek = ld.getDayOfWeek().getValue();
        System.out.println(year);
        System.out.println(month);
        System.out.println(day);
        System.out.println(dayFoYear);
        System.out.println(dayFoWeek);


        //改写时间
        LocalDate ld2 = ld.withYear(2009);
        LocalDate ld3 = ld.withMonth(3);
        System.out.println(ld2);
        System.out.println(ld3);


        //增添时间
        LocalDate ld4 = ld.plusYears(1);
        LocalDate ld5 = ld.plusMonths(1);
        System.out.println(ld4);
        System.out.println(ld5);


        //缩减时间
        LocalDate ld6 = ld.minusYears(1);
        LocalDate ld7 = ld.minusMonths(1);
        System.out.println(ld6);
        System.out.println(ld7);


        //设定固定时间对象
        LocalDate ld8 = LocalDate.of(2099,12,12);
        LocalDate ld9 = LocalDate.of(2099,12,12);


        //对比时间
        System.out.println(ld9.equals(ld8));
        System.out.println(ld9.isAfter(ld8));
        System.out.println(ld9.isBefore(ld8));



        //获取时间戳
        Instant  i =  Instant.now();
        System.out.println(i.getEpochSecond());


        //时间格式化
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        LocalDate myLd = LocalDate.now();
        System.out.println(myLd);


        System.out.println(myLd.format(dtf));//反向格式化

        System.out.println(dtf.format(myLd));//正向格式化

        //解析时间
        String s = "2020年10月10日";
        LocalDate lo = LocalDate.parse(s,dtf);
        System.out.println(lo);


        LocalDate fristD = LocalDate.now();
        LocalDate secondD = LocalDate.of(2020,12,1);


        Period pd = Period.between(secondD,fristD);
        System.out.println(pd.getYears());
        System.out.println(pd.getMonths());
        System.out.println(pd.getDays());


        LocalDateTime fristT = LocalDateTime.now();
        LocalDateTime secondT = LocalDateTime.of(2020,12,1,0,0,0);

        Duration d = Duration.between(secondT,fristT);
        System.out.println(d.toDays());
        System.out.println(d.toHours());
        System.out.println(d.toMillis());

    }
}
